<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : UserBill.php
 * Created on : 2023-4-4 10:45:56
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace app\models;

use think\Model;
use haoteam\traits\ModelTrait;

/**
 * 
 * Class UserBill
 * Tiger <1192851302@qq.com> 2023-4-4 10:45:56
 */
class UserBill extends Model {

    use ModelTrait;

    /**
     * 支出账单写入
     * @author Tiger <1192851302@qq.com>
     * @param type $user_id     用户ID
     * @param type $link_id     关联ID
     * @param type $title       标题
     * @param type $category    类别 money余额，integral积分
     * @param type $type        类型 withdraw 提现 withdraw_refuse提现返回 recharge充值 buy_product 购买商品 buy_refund 退款 invest_income 投资收益 agent_income 代理收益
     * @param type $number      发送数量
     * @param type $balance     结余
     * @param type $mark        备注
     * @return boolean
     */
    public static function expend($user_id, $link_id, $title, $category, $type, $number, $balance, $status = 0, $mark = '') {
        $pm = 0;
        return self::create(compact('user_id', 'link_id', 'title', 'category', 'type', 'number', 'balance', 'mark', 'status', 'pm'));
    }

    /**
     * 收入账单写入
     * @author Tiger <1192851302@qq.com>
     * @param type $user_id     用户ID
     * @param type $link_id     关联ID
     * @param type $title       标题
     * @param type $category    类别 money 余额，integral 积分
     * @param type $type        类型 withdraw 提现 withdraw_refuse提现拒绝 recharge充值 buy_product 购买商品 buy_refund 退款 invest_income 投资收益 agent_income 代理收益
     * @param type $number      发送数量
     * @param type $balance     结余
     * @param type $mark        备注
     * @return boolean
     */
    public static function income($user_id, $link_id, $title, $category, $type, $number, $balance, $status = 0, $mark = '') {
        $pm = 1;
        return self::create(compact('user_id', 'link_id', 'title', 'category', 'type', 'number', 'balance', 'mark', 'status', 'pm'));
    }

    /**
     * 
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public static function validWhere() {
        return self::where('status', '<>', -1);
    }

    /**
     * 获取数据列表-分页
     * @author Tiger <1192851302@qq.com>
     * @param type $where
     * @param type $date
     * @return type
     */
    public static function getList($where, $date = null) {
        $model = self::validWhere();
        if (!empty($where['search'])) {
            $model->where('title', 'LIKE', '%' . $where['search'] . '%');
        }
        unset($where['search']);
        if ($date == 'trid') {
            $model->whereTime('time', '>=', '-3 day');
        }
        if ($date == 'week') {
            $model->whereWeek('time');
        }
        if ($date == 'month') {
            $model->whereMonth('time');
        }
        if ($date == 'last_month') {
            $model->whereMonth('time', 'last month');
        }
        $model->where($where)->order('time desc');
        $models = $model->paginate(input('limit', 10));
        return [
            'code' => 0,
            'items' => $models->items(),
            'total' => $models->total()
        ];
    }

}
